#!/usr/bin/python

from PIL import Image

from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfgen import canvas
from reportlab.lib.colors import black, white, gray, lightgrey
from reportlab.lib.pagesizes import letter
from reportlab.lib.utils import ImageReader, LazyImageReader
from reportlab.platypus.tables import LINECAPS


from perks import armorPerks, auxPerks, defensePerks, ewPerks, meleePerks
from perks import miscPerks, movementPerks, rangedPerks

cWidth = 252
cHeight  = 180
padding = 2
LINE_WIDTH = 0.1

stripeImage = "images/hazard_stripe.png"
infoImages = {
    "movement" : "images/arrows/arrow_wb.png",
    "ew" : "images/arrows/arrow_wb.png",    
    "ranged" : "images/arrows/arrow_wb.png",
    "armor" : "images/arrows/arrow_wb.png",
    "defense" : "images/arrows/arrow_wb.png",
    "melee" : "images/arrows/arrow_wb.png",
    "aux" : "images/arrows/arrow_wb.png",
    "misc" : "images/arrows/arrow_wb.png",
}
factionImages = {
    "north": "images/icons/icon_north.png",
    "south": "images/icons/icon_south.png",
    "prdf": "images/icons/icon_prdf.png",
    "pak": "images/icons/icon_pak.png",
}

pdfmetrics.registerFont(TTFont('LS-Bold', 'fonts/LiberationSans-Bold.ttf'))
pdfmetrics.registerFont(TTFont('LS', 'fonts/LiberationSans-Regular.ttf'))

def drawBorder(canvas, origin):
    canvas.setLineWidth(LINE_WIDTH)
    canvas.rect(0,0, cWidth, cHeight)

def drawBackground(canvas, origin):
    stripe = ImageReader(stripeImage)
    canvas.drawImage(stripe, origin[0] + 0, origin[1] + 0, width=20, height=50)
    canvas.drawImage(stripe, origin[0] + 0, origin[1] + 49, width=20, height=50)
    canvas.drawImage(stripe, origin[0] + 0, origin[1] + 98, width=20, height=50)
    canvas.drawImage(stripe, origin[0] + 0, origin[1] + 147, width=20, height=50)
    
    iconImage = ImageReader(factionImages['north'])
    #linelist = [
    #    ( 35,  cHeight-2, 194, cHeight-2),
    #    ( 35,  cHeight-2,  35, cHeight-16),
    #    ( 35, cHeight-16, 194, cHeight-16),
    #    (194,  cHeight-2, 194, cHeight-16),
    #]    
    #canvas.setLineCap(LINECAPS['round'])
    #canvas.lines(linelist)
    canvas.drawImage(iconImage, 2, cHeight-34,
                     mask=(0,0,0,0,0,0), width=32, height=32)

def drawUnitPicture(canvas, origin):
    unit = ImageReader("images/units/LHT67.png")
    #unit = ImageReader("images/units/Naga.png")
    canvas.setLineCap(LINECAPS['round'])
    canvas.setStrokeColor(lightgrey)
    canvas.rect(origin[0] + 36, (origin[1] + cHeight)-69, 64, 64)
    canvas.drawImage(unit, origin[0] + 37, (origin[1] + cHeight-70), height=64, width=64,
                     preserveAspectRatio=True, anchor="c",
                     mask=(61,61,213,213,20,20),
                     )
    
def drawTitle(canvas, origin):
    
    canvas.setFont("LS-Bold", 9)    
    canvas.drawRightString(cWidth-2,cHeight-9, "LHT-67 Prowler-class Hovertank")
    titleHeight = (origin[1] + cHeight) - 11
    canvas.setStrokeColor(black)
    canvas.line((origin[0] + cWidth ) - 140, titleHeight+1, (origin[0] + cWidth) - padding, titleHeight+1)
    
    canvas.setFont("LS-Bold", 12)
    canvas.setFillColor(white)
    canvas.rotate(90)
    canvas.drawString(origin[0] + 4, origin[1] - 14, "HGB-FE")
    canvas.rotate(-90)

# Older methods below here

def drawArmor(canvas, origin):
    
    boxLeftX = cWidth-34
    boxWidth = 32
    boxHeight = (origin[0]) + 70
    boxTopY = (origin[1]) + boxHeight + padding

    # Draw the border first
    canvas.setStrokeColor(black)
    canvas.setLineCap(LINECAPS['round'])
    canvas.setFillColor(white)
    canvas.roundRect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, radius=1, stroke=1, fill=1)
    canvas.setFillColor(black)

    # Create the black text box
    canvas.roundRect(boxLeftX, boxTopY - 6, boxWidth, 6, radius=1, stroke=0, fill=1)
    canvas.line(boxLeftX, boxTopY - 6, boxLeftX + boxWidth, boxTopY - 6)
    canvas.setFillColor(white)
    canvas.setFont("LS", 6)
    canvas.drawString(boxLeftX + 2, boxTopY - 5, "Armor")
    
    # Insert our 'turn page' image if necessary
    moreInfo = ImageReader(infoImages["armor"])
    canvas.drawImage(moreInfo, boxLeftX + boxWidth - padding - 3, boxTopY - 5,
                     width=4, height=4, mask=(0,0,0,0,0,0))
    
    # Now create the boxes; should be 6 pixels square    
    canvas.setStrokeColor(black)
    canvas.setFont("LS", 5)
    canvas.setFillColor(black)
    
    boxDims = 8
    boxLeft = (cWidth - 18) - (boxDims / 2)
    boxSpacer = 2
    fontSpace = 6
    
    # Box 1
    boxBottom = boxTopY - 5 - padding - boxDims - boxSpacer # 5 is title + padding
    canvas.setStrokeColor(lightgrey)
    canvas.setFillColor(lightgrey)
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "-")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "-")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "-")

    # Box 2
    boxBottom = boxBottom - boxSpacer - boxDims
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)    
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "-")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "-")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "-")

    # Box 3
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
    boxBottom = boxBottom - boxSpacer - boxDims
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)    
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "S")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "22")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "22")

    # Box 4
    boxBottom = boxBottom - boxSpacer - boxDims
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)    
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "L")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "44")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "44")

    # Box 5
    boxBottom = boxBottom - boxSpacer - boxDims
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)    
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "H")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "66")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "66")

    # Box 6
    boxBottom = boxBottom - boxSpacer - boxDims
    canvas.rect(boxLeft, boxBottom, boxDims, boxDims)    
    canvas.drawCentredString(boxLeft + 4, boxBottom+2, "C")
    canvas.drawCentredString(boxLeft - fontSpace, boxBottom + 2, "88")
    canvas.drawCentredString(boxLeft + boxDims + fontSpace, boxBottom + 2, "88")

def drawMovement(canvas, origin):
    print "Entered drawMovement"
    # Define our diminsions
    origin_x = origin[0]
    origin_y = origin[1]
    boxWidth = 80
    boxHeight = 24
    boxLeftX = (origin_x + cWidth) - (boxWidth + padding + 68)
    boxBottomY = (origin_y + cHeight) - boxHeight - 11
    
    moveTypeX = boxLeftX + 2
    moveCbtX = moveTypeX + 28
    moveSlashX = moveCbtX + 1
    moveTopX = moveSlashX + 1
    
    turnStaX = moveTopX + 16
    turnCbtX = turnStaX + 12
    turnTopX = turnCbtX + 12
    
    # Draw title first
    titleHeight = 5
    titleY = boxBottomY + boxHeight - 5
    canvas.setFillColor(black)
    canvas.rect(boxLeftX, titleY, boxWidth, titleHeight, stroke=0, fill=1)
    canvas.setFillColor(white)
    canvas.setFont("LS-Bold", 4)
    canvas.drawString(boxLeftX + padding, titleY + 1, "MOVEMENT")
    moreInfo = ImageReader(infoImages["movement"])
    canvas.drawImage(moreInfo, boxLeftX + boxWidth - padding - 3, titleY + 1,
                     width=3, height=3, mask=(0,0,0,0,0,0))
    
    # Now header
    headerHeight = 5
    headerY = titleY - headerHeight
    canvas.setFillColor(white)
    canvas.setStrokeColor(black)
    canvas.rect(boxLeftX, headerY, boxWidth, headerHeight, stroke=1, fill=0)
    canvas.setFillColor(black)
    canvas.setFont("LS", 4)
    canvas.drawString(moveTypeX, headerY + 1 , "TYPE")
    canvas.drawCentredString(moveSlashX, headerY + 1, "SPEED")
    canvas.drawCentredString(turnCbtX, headerY + 1, "TURNS")    
    
    # Draw the data box
    canvas.setFillColor(white)
    canvas.setStrokeColor(black)
    lineHeight = 6
    boxHeight = headerY - boxBottomY
    canvas.rect(boxLeftX, boxBottomY, boxWidth, boxHeight, stroke=1, fill=1)

    priY = headerY - headerHeight
    secY = priY - lineHeight 

    canvas.setFillColor(black)
    canvas.setFont("LS", 5)
    # Primary Movement
    canvas.drawString(moveTypeX, priY, "Hover")
    canvas.drawCentredString(moveSlashX, priY, "/")
    canvas.drawString(moveTopX, priY, "26")
    canvas.drawRightString(turnStaX - 1, priY, "3")
    canvas.drawCentredString(turnStaX, priY, "/")
    canvas.drawString(turnStaX + 1, priY, "0")
    canvas.drawRightString(turnTopX - 1, priY, "1")
    canvas.drawCentredString(turnTopX, priY, "/")
    canvas.drawString(turnTopX + 1, priY, "4")

    # Secondary Movement
    canvas.setFont("LS", 5)
    canvas.drawString(moveTypeX, secY, "Walker")
    canvas.drawCentredString(moveSlashX, secY, "/")
    canvas.drawString(moveTopX, secY, "11")
    canvas.drawRightString(turnStaX - 1, secY, "")
    canvas.drawCentredString(turnStaX, secY, "U")
    canvas.drawString(turnStaX + 1, secY, "")
    canvas.drawRightString(turnTopX - 1, secY, "1")
    canvas.drawCentredString(turnTopX, secY, "/")
    canvas.drawString(turnTopX + 1, secY, "2")

    canvas.setFont("LS-Bold", 5)
    # Primary Movement
    canvas.drawRightString(moveCbtX, priY, "13")
    canvas.drawRightString(turnCbtX - 1, priY, "1")
    canvas.drawCentredString(turnCbtX, priY, "/")
    canvas.drawString(turnCbtX + 1, priY, "2")

    # Secondary Movement
    canvas.drawRightString(moveCbtX, secY, "6")
    canvas.drawRightString(turnCbtX - 1, secY, "")
    canvas.drawCentredString(turnCbtX, secY, "U")
    canvas.drawString(turnCbtX + 1, secY, "")
    
    # Draw the perks box
    perk_list = []
    for perk in movementPerks.values():
        perk_list.append(perk[1])
    drawPerksBox(canvas, perk_list, (boxLeftX, boxBottomY), boxWidth)

def drawDefense(canvas, origin):
    origin_x = origin[0]
    origin_y = origin[1]

    # Draw the border first
    boxWidth = 66
    boxHeight = 30
    boxLeftX = (origin_x + cWidth) - padding - boxWidth
    
    boxBottomY = (origin_y + cHeight) - boxHeight - 11
    boxTitleBottomY = boxBottomY + boxHeight - 5
    boxHeaderBottomY = boxTitleBottomY - 4
    
    # Title bar first
    canvas.setFillColor(black)
    canvas.rect(boxLeftX, boxTitleBottomY, boxWidth, 5, stroke=0, fill=1)
    canvas.setFillColor(white)
    canvas.setFont("LS-Bold", 4)
    canvas.drawString(boxLeftX + padding, boxTitleBottomY + 1, "DEFENSE")

    canvas.setStrokeColor(black)
    canvas.setFillColor(white)
    canvas.line(boxLeftX, boxTitleBottomY, boxLeftX+boxWidth, boxTitleBottomY)
    canvas.rect(boxLeftX, boxBottomY, boxWidth, boxHeight, stroke=1)
        
    # Insert our 'turn page' image if necessary
    moreInfo = ImageReader(infoImages["movement"])
    canvas.drawImage(moreInfo, boxLeftX + boxWidth - padding - 3, boxTitleBottomY +1,
                     width=3, height=3, mask=(0,0,0,0,0,0))
    
    canvas.setFont("LS", 4)
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
    
    moveTypeX = boxLeftX + 2   
    modTopX = boxLeftX + boxWidth - padding - 4
    modCbtX = modTopX - 10
    modStopX = modCbtX - 10
    lineHeight = 4 + padding
    atkY = boxBottomY + padding +1
    secY = atkY + lineHeight
    priY = secY + lineHeight
    
    # Labels
    canvas.setFillColor(black)
    canvas.setFont("LS-Bold", 4)
    canvas.drawCentredString(modCbtX, boxHeaderBottomY, "CBT")
    
    canvas.setFont("LS", 4)
    canvas.drawString(moveTypeX, boxHeaderBottomY, "TYPE")
    canvas.drawCentredString(modStopX, boxHeaderBottomY, "STA")
    canvas.drawCentredString(modTopX, boxHeaderBottomY, "TOP")

    # Draw the rect for the Combat speed zebra stripe
    canvas.setFillColor(lightgrey)
    zebraStripeWidth = 8
    canvas.rect(modCbtX - (zebraStripeWidth/2), atkY - 2, zebraStripeWidth, (boxHeaderBottomY - atkY +1), stroke=0, fill=1)
    
    # And then the header split bar
    canvas.line(boxLeftX, boxHeaderBottomY-1, boxLeftX + boxWidth, boxHeaderBottomY-1)

    # Primary Movement
    canvas.setFillColor(black)
    canvas.setFont("LS", 5)
    canvas.drawString(moveTypeX, priY, "Ground")
    canvas.drawCentredString(modStopX, priY, "-4")
    canvas.drawCentredString(modTopX, priY, "+2")

    # Secondary Movement
    canvas.drawString(moveTypeX, secY, "Walker")  
    canvas.drawCentredString(modStopX, secY, "-1")
    canvas.drawCentredString(modTopX, secY, "+4")

    # Attack Modifers
    canvas.drawString(moveTypeX, atkY, "Attack")   
    canvas.drawCentredString(modStopX, atkY, "+2")
    canvas.drawCentredString(modTopX, atkY, "-3")

    canvas.setFont("LS-Bold", 5)
    canvas.drawCentredString(modCbtX, priY, "+1")
    canvas.drawCentredString(modCbtX, secY, "+2")
    canvas.drawCentredString(modCbtX, atkY, "+0")
    

    # Draw the perks box
    perk_list = []
    for perk in defensePerks.values():
        perk_list.append(perk[1])
    drawPerksBox(canvas, perk_list, (boxLeftX, boxBottomY), boxWidth)

def drawEW(canvas, origin):
    print "Drawing EW with origin at (%i,%i)" % (origin[0], origin[1])
    
    boxWidth = 80
    boxHeight = 24
    boxLeftX = (origin[0] + cWidth) - (boxWidth + padding + 68) 
    boxBottomY = (origin[1] + cHeight) - boxHeight - 44
    boxTitleBottomY = boxBottomY + boxHeight - 4

    # Line positions
    lineWidth = 3 + padding
    lineThreeY = boxBottomY + padding +1
    lineTwoY   = lineThreeY + lineWidth + 1
    lineOneY   = lineTwoY + lineWidth + 1

    labelOneX = boxLeftX + padding
    labelTwoX = labelOneX + (boxWidth/2) - padding
    valueOneX = labelTwoX - padding
    valueTwoX = boxLeftX + boxWidth - padding

    canvas.setFillColor(black)
    canvas.rect(boxLeftX, boxBottomY, boxWidth, boxHeight, stroke=1) # outline
    canvas.rect(boxLeftX, boxTitleBottomY, boxWidth, 4 + (padding/2), stroke=0, fill=1) # black title

    canvas.setFillColor(white)
    canvas.setFont("LS-Bold", 4)
    canvas.drawString(boxLeftX + padding, boxTitleBottomY + (padding/2), "EW")
        
    # Insert our 'turn page' image if necessary
    moreInfo = ImageReader(infoImages["aux"])
    canvas.drawImage(moreInfo, boxLeftX + boxWidth - padding - 3, boxTitleBottomY + (padding/2),
                     width=3, height=3, mask=(0,0,0,0,0,0))

    # Draw the Values
    canvas.setFillColor(black)
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(valueOneX, lineOneY, "3")
    canvas.drawRightString(valueTwoX, lineOneY, "4")
    canvas.drawRightString(valueOneX, lineTwoY, "+1")
    canvas.drawRightString(valueTwoX, lineTwoY, "+2")
    canvas.setFillColor(lightgrey)
    canvas.drawRightString(valueOneX, lineThreeY, "+0")
    canvas.drawRightString(valueTwoX, lineThreeY, "+0")
    canvas.setFillColor(black)
    
    # Draw the Labels
    canvas.setFont("LS", 5)
    canvas.drawString(labelOneX, lineOneY, "Detect:")
    canvas.drawString(labelTwoX, lineOneY, "Auto Comm:")
    canvas.drawString(labelOneX, lineTwoY, "Sensors:")
    canvas.drawString(labelTwoX, lineTwoY, "Comm:")
    canvas.setFillColor(lightgrey)
    canvas.drawString(labelOneX, lineThreeY, "ECM:")
    canvas.drawString(labelTwoX, lineThreeY, "ECCM:")
    canvas.setFillColor(black)

    # Draw the perks box
    perk_list = []
    for perk in ewPerks.values():
        perk_list.append(perk[1])
    drawPerksBox(canvas, perk_list, (origin[0] + boxLeftX, origin[1] + boxBottomY), boxWidth)
    
def drawRangedWeapons(canvas, origin):
    
    boxLeftX = 22
    boxWidth = 194
    boxHeight = (origin[0]) + 50
    boxTopY = (origin[1]) + boxHeight + padding + 34
    
    # Draw our crosshatching
    startY = boxTopY-5-6-8 # 8 for the title bar, 5 for the header, 6 for spacing
    endY = startY - boxHeight + 5 + 8 # 8 for the title bar, 5 for the header
    zebraStripeHeight = 6
    canvas.setLineCap(LINECAPS['squared'])    
    for stripe in range((startY - endY) / zebraStripeHeight):        
        #print "Stripe is: ", stripe
        if stripe % 2 == 0:
            canvas.setFillColor(lightgrey)
        else:
            canvas.setFillColor(white)
        canvas.rect(boxLeftX, startY - (zebraStripeHeight*stripe), boxWidth,
                    zebraStripeHeight, stroke=0, fill=1)
    
    #canvas.setLineCap(LINECAPS['round'])
    canvas.setLineWidth(0.1)
    canvas.setLineJoin(0)
    canvas.setLineCap(0)
    #canvas.roundRect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, radius=1)
    canvas.rect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight)
    
    canvas.setFillColor(black)
    canvas.roundRect(boxLeftX, boxTopY-6, boxWidth, 6, radius=1, stroke=0, fill=1)
    #canvas.rect(boxLeftX, boxTopY-6, boxWidth, 6, stroke=0, fill=1)
    canvas.line(boxLeftX, boxTopY-6, boxLeftX+boxWidth, boxTopY-6)
    canvas.setFillColor(white)
    
    canvas.setFont("LS-Bold", 4)
    canvas.drawString(boxLeftX + padding, boxTopY-5, "RANGED WEAPONS")
    
    nameX = boxLeftX + padding
    arcX = nameX + 22
    accX = arcX + 16
    
    pbX = accX + 14
    pbSlashX = pbX +1
    shortX = pbX + 12
    shortSlashX = shortX + 1
    mediumX = shortX + 12
    mediumSlashX = mediumX + 1
    longX = mediumX + 12
    longSlashX = longX + 1
    extremeX = longX + 12
    
    rangeLabelX = mediumX
    damX = extremeX + 4
    specialX = damX + 12
    ammoX = boxLeftX + boxWidth - padding
    
    # Draw the Header
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
    canvas.setFont("LS", 4)
    headerY = boxTopY-5-6

    canvas.line(boxLeftX, headerY-padding, boxLeftX+boxWidth, headerY-padding)
    canvas.drawString(nameX, headerY, "NAME")
    canvas.drawString(arcX, headerY, "ARC")
    canvas.drawCentredString(accX, headerY, "ACC")
    canvas.drawCentredString(rangeLabelX, headerY, "RANGE")
    canvas.drawString(damX, headerY, "DAM")
    canvas.drawString(specialX, headerY, "SPECIAL")
    canvas.setFont("LS", 5)
        
    # Draw Weapon 1
    weaponOneY = headerY - 7
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)    
    canvas.drawString(nameX, weaponOneY, "HRG")
    canvas.drawString(arcX, weaponOneY, "T")
    canvas.drawCentredString(accX, weaponOneY, "+0")
    
    canvas.drawRightString(pbX, weaponOneY, "15")
    canvas.drawString(pbSlashX, weaponOneY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponOneY, "30")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponOneY, "/")
    canvas.drawRightString(mediumX, weaponOneY, "60")
    canvas.drawString(mediumSlashX, weaponOneY, "/")
    canvas.drawRightString(longX, weaponOneY, "120")
    canvas.drawString(longSlashX, weaponOneY, "/")
    canvas.drawRightString(extremeX, weaponOneY, "240")
    
    canvas.drawString(damX, weaponOneY, "X35")
    canvas.drawString(specialX, weaponOneY, "")
    canvas.drawRightString(ammoX, weaponOneY, "O"*6)
    
    # Draw Weapon 2
    weaponTwoY = weaponOneY - 6
    canvas.drawString(nameX, weaponTwoY, "LPLC")
    canvas.drawString(arcX, weaponTwoY, "T")
    canvas.drawCentredString(accX, weaponTwoY, "+1")
    canvas.drawRightString(pbX, weaponTwoY, "5")
    canvas.drawString(pbSlashX, weaponTwoY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponTwoY, "9")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponTwoY, "/")
    canvas.drawRightString(mediumX, weaponTwoY, "18")
    canvas.drawString(mediumSlashX, weaponTwoY, "/")
    canvas.drawRightString(longX, weaponTwoY, "36")
    canvas.drawString(longSlashX, weaponTwoY, "/")
    canvas.drawRightString(extremeX, weaponTwoY, "72")
    canvas.drawString(damX, weaponTwoY, "X20")
    canvas.drawString(specialX, weaponTwoY, "MB3,AA")
    canvas.drawRightString(ammoX, weaponTwoY, "O"*6)    
    
    # Draw Weapon 3
    weaponThreeY = weaponTwoY - 6
    canvas.drawString(nameX, weaponThreeY, "MAC")
    canvas.drawString(arcX, weaponThreeY, "T")
    canvas.drawCentredString(accX, weaponThreeY, "+0")
    canvas.drawRightString(pbX, weaponThreeY, "5")
    canvas.drawString(pbSlashX, weaponThreeY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponThreeY, "9")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponThreeY, "/")
    canvas.drawRightString(mediumX, weaponThreeY, "18")
    canvas.drawString(mediumSlashX, weaponThreeY, "/")
    canvas.drawRightString(longX, weaponThreeY, "36")
    canvas.drawString(longSlashX, weaponThreeY, "/")
    canvas.drawRightString(extremeX, weaponThreeY, "72")
    canvas.drawString(damX, weaponThreeY, "x10")
    canvas.drawString(specialX, weaponThreeY, "ROF 1")
    canvas.drawRightString(ammoX, weaponThreeY, "O"*6)
    
    # Draw Weapon 4
    weaponFourY = weaponThreeY - 6
    canvas.drawString(nameX, weaponFourY, "HATM")
    canvas.drawString(arcX, weaponFourY, "T")
    canvas.drawCentredString(accX, weaponFourY, "+1")
    canvas.drawRightString(pbX, weaponFourY, "5")
    canvas.drawString(pbSlashX, weaponFourY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponFourY, "9")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponFourY, "/")
    canvas.drawRightString(mediumX, weaponFourY, "18")
    canvas.drawString(mediumSlashX, weaponFourY, "/")
    canvas.drawRightString(longX, weaponFourY, "36")
    canvas.drawString(longSlashX, weaponFourY, "/")
    canvas.drawRightString(extremeX, weaponFourY, "72")
    canvas.drawString(damX, weaponFourY, "X30")
    canvas.drawString(specialX, weaponFourY, "IF,G")
    canvas.drawRightString(ammoX, weaponFourY, "O"*6)
    
    # Draw Weapon 5
    weaponFiveY = weaponFourY - 6
    canvas.drawString(nameX, weaponFiveY, "HMG")
    canvas.drawString(arcX, weaponFiveY, "T")
    canvas.drawCentredString(accX, weaponFiveY, "+0")
    canvas.drawRightString(pbX, weaponFiveY, "2")
    canvas.drawString(pbSlashX, weaponFiveY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponFiveY, "3")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponFiveY, "/")
    canvas.drawRightString(mediumX, weaponFiveY, "6")
    canvas.drawString(mediumSlashX, weaponFiveY, "/")
    canvas.drawRightString(longX, weaponFiveY, "12")
    canvas.drawString(longSlashX, weaponFiveY, "/")
    canvas.drawRightString(extremeX, weaponFiveY, "24")
    canvas.drawString(damX, weaponFiveY, "X4")
    canvas.drawString(specialX, weaponFiveY, "ROF 3,AI")
    canvas.drawRightString(ammoX, weaponFiveY, "O"*6)
    
    # Draw Weapon 6
    weaponSixY = weaponFiveY - 6
    canvas.drawString(nameX, weaponSixY, "HAAC")
    canvas.drawString(arcX, weaponSixY, "T")
    canvas.drawCentredString(accX, weaponSixY, "+0")
    canvas.drawRightString(pbX, weaponSixY, "5")
    canvas.drawString(pbSlashX, weaponSixY, "/")
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponSixY, "9")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponSixY, "/")
    canvas.drawRightString(mediumX, weaponSixY, "18")
    canvas.drawString(mediumSlashX, weaponSixY, "/")
    canvas.drawRightString(longX, weaponSixY, "36")
    canvas.drawString(longSlashX, weaponSixY, "/")
    canvas.drawRightString(extremeX, weaponSixY, "72")
    canvas.drawString(damX, weaponSixY, "X12")
    canvas.drawString(specialX, weaponSixY, "ROF 3,AA")
    canvas.drawRightString(ammoX, weaponSixY, "O"*6)

    # Draw Weapon 7
    #weaponSevenY = weaponSixY - 6
    #canvas.drawString(nameX, weaponSevenY, "MRP/36")
    #canvas.drawString(arcX, weaponSevenY, "FRr")
    #canvas.drawCentredString(accX, weaponSevenY, "-1")
    #canvas.drawRightString(pbX, weaponSevenY, "3")
    #canvas.drawString(pbSlashX, weaponSevenY, "/")
    #canvas.setFont("LS-Bold", 5)
    #canvas.drawRightString(shortX, weaponSevenY, "6")
    #canvas.setFont("LS", 5)
    #canvas.drawString(shortSlashX, weaponSevenY, "/")
    #canvas.drawRightString(mediumX, weaponSevenY, "12")
    #canvas.drawString(mediumSlashX, weaponSevenY, "/")
    #canvas.drawRightString(longX, weaponSevenY, "24")
    #canvas.drawString(longSlashX, weaponSevenY, "/")
    #canvas.drawRightString(extremeX, weaponSevenY, "48")
    #canvas.drawString(damX, weaponSevenY, "X18")
    #canvas.drawString(specialX, weaponSevenY, "ROF 4,IF")
    #canvas.drawRightString(ammoX, weaponSevenY, "O"*6)

def drawThrownWeapons(canvas, origin):
    boxLeftX = 22
    boxWidth = 106
    boxHeight = (origin[0]) + 32
    boxTopY = (origin[1]) + boxHeight + padding
    
    # Draw our crosshatching
    startY = boxTopY-5-6-8 # 8 for the title bar, 5 for the header, 6 for spacing
    endY = startY - boxHeight + 5 + 8 # 8 for the title bar, 5 for the header
    zebraStripeHeight = 6
    canvas.setLineCap(LINECAPS['squared'])    
    for stripe in range((startY - endY) / zebraStripeHeight):        
        #print "Stripe is: ", stripe
        if stripe % 2 == 0:
            canvas.setFillColor(lightgrey)
        else:
            canvas.setFillColor(white)
        canvas.rect(boxLeftX, startY - (zebraStripeHeight*stripe), boxWidth,
                    zebraStripeHeight, stroke=0, fill=1)
    
    canvas.setLineCap(LINECAPS['round'])    
    canvas.roundRect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, radius=1)
    
    canvas.setFillColor(black)
    canvas.roundRect(boxLeftX, boxTopY-6, boxWidth, 6, radius=1, stroke=0, fill=1)
    canvas.line(boxLeftX, boxTopY-6, boxLeftX+boxWidth, boxTopY-6)
    canvas.setFillColor(white)
    
    canvas.setFont("LS", 6)
    canvas.drawString(boxLeftX + padding, boxTopY-5, "Thrown Weapons")
    
    nameX = boxLeftX + padding
    accX = nameX + 22
    
    shortX = accX + 12
    shortSlashX = shortX + 1
    mediumX = shortX + 8
    mediumSlashX = mediumX + 1
    
    rangeLabelX = shortSlashX
    damX = mediumX + 4
    specialX = damX + 12
    ammoX = boxLeftX + boxWidth - padding
    
    # Draw the Header
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
    canvas.setFont("LS", 4)
    headerY = boxTopY-5-6
    canvas.line(boxLeftX, headerY-padding, boxLeftX+boxWidth, headerY-padding)
    canvas.drawString(nameX, headerY, "NAME")    
    canvas.drawCentredString(accX, headerY, "ACC")
    canvas.drawCentredString(rangeLabelX, headerY, "RANGE")
    canvas.drawString(damX, headerY, "DAM")
    canvas.drawString(specialX, headerY, "SPECIAL")
    canvas.setFont("LS", 5)
    
    # Draw Weapon 1
    weaponOneY = headerY - 7
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)    
    canvas.drawString(nameX, weaponOneY, "HG")    
    canvas.drawCentredString(accX, weaponOneY, "-1")
    
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponOneY, "6")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponOneY, "/")    
    canvas.drawRightString(mediumX, weaponOneY, "12")
    
    canvas.drawString(damX, weaponOneY, "X15")
    canvas.drawString(specialX, weaponOneY, "AI,AE2")
    canvas.drawRightString(ammoX, weaponOneY, "O"*6)
    
    # Draw Weapon 2
    weaponTwoY = weaponOneY - 6
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)    
    canvas.drawString(nameX, weaponTwoY, "HHG")    
    canvas.drawCentredString(accX, weaponTwoY, "-1")
    
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponTwoY, "6")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponTwoY, "/")    
    canvas.drawRightString(mediumX, weaponTwoY, "12")
    
    canvas.drawString(damX, weaponTwoY, "X25")
    canvas.drawString(specialX, weaponTwoY, "AE2")
    canvas.drawRightString(ammoX, weaponTwoY, "O"*6)
    
    # Draw Weapon 3
    weaponThreeY = weaponTwoY - 6
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)
    canvas.drawString(nameX, weaponThreeY, "HWG")
    canvas.drawCentredString(accX, weaponThreeY, "-1")
    
    canvas.setFont("LS-Bold", 5)
    canvas.drawRightString(shortX, weaponThreeY, "6")
    canvas.setFont("LS", 5)
    canvas.drawString(shortSlashX, weaponThreeY, "/")    
    canvas.drawRightString(mediumX, weaponThreeY, "12")
    
    canvas.drawString(damX, weaponThreeY, "X10")
    canvas.drawString(specialX, weaponThreeY, "H,AE1")
    canvas.drawRightString(ammoX, weaponThreeY, "O"*6)
    
def drawMeleeWeapons(canvas, origin):
    boxLeftX = 130
    boxWidth = 86
    boxHeight = (origin[0]) + 32
    boxTopY = (origin[1]) + boxHeight + padding
    
    # Draw our crosshatching
    startY = boxTopY-5-6-8 # 8 for the title bar, 5 for the header, 6 for spacing
    endY = startY - boxHeight + 5 + 8 # 8 for the title bar, 5 for the header
    zebraStripeHeight = 6
    canvas.setLineCap(LINECAPS['squared'])    
    for stripe in range((startY - endY) / zebraStripeHeight):        
        #print "Stripe is: ", stripe
        if stripe % 2 == 0:
            canvas.setFillColor(lightgrey)
        else:
            canvas.setFillColor(white)
        canvas.rect(boxLeftX, startY - (zebraStripeHeight*stripe), boxWidth,
                    zebraStripeHeight, stroke=0, fill=1)
    
    canvas.setLineCap(LINECAPS['round'])    
    canvas.roundRect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, radius=1)
    
    canvas.setFillColor(black)
    canvas.roundRect(boxLeftX, boxTopY-6, boxWidth, 6, radius=1, stroke=0, fill=1)
    canvas.line(boxLeftX, boxTopY-6, boxLeftX+boxWidth, boxTopY-6)
    canvas.setFillColor(white)
    
    canvas.setFont("LS", 6)
    canvas.drawString(boxLeftX + padding, boxTopY-5, "Melee Weapons")
    
    nameX = boxLeftX + padding
    accX = nameX + 34
    
    damX = accX + 8
    specialX = damX + 12
    ammoX = boxLeftX + boxWidth - padding
    
    # Draw the Header
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
    canvas.setFont("LS", 4)
    headerY = boxTopY-5-6
    canvas.line(boxLeftX, headerY-padding, boxLeftX+boxWidth, headerY-padding)
    canvas.drawString(nameX, headerY, "NAME")    
    canvas.drawCentredString(accX, headerY, "ACC")
    canvas.drawString(damX, headerY, "DAM")
    canvas.drawString(specialX, headerY, "SPECIAL")
    canvas.setFont("LS", 5)
    
    # Draw Weapon 1
    weaponOneY = headerY - 7
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)    
    canvas.drawString(nameX, weaponOneY, "HVS")    
    canvas.drawCentredString(accX, weaponOneY, "-1")
    canvas.drawString(damX, weaponOneY, "X16")
    canvas.drawString(specialX, weaponOneY, "AC")
    #canvas.drawRightString(ammoX, weaponOneY, "O"*6)
    
    # Draw Weapon 2
    weaponTwoY = weaponOneY - 6
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)    
    canvas.drawString(nameX, weaponTwoY, "PUNCH/KICK")    
    canvas.drawCentredString(accX, weaponTwoY, "+0")
    canvas.drawString(damX, weaponTwoY, "X8")
    canvas.drawString(specialX, weaponTwoY, "")
    canvas.drawRightString(ammoX, weaponTwoY, "O"*4)
    
    # Draw Weapon 3
    weaponThreeY = weaponTwoY - 6
    canvas.setFillColor(black)
    canvas.setStrokeColor(black)
    canvas.drawString(nameX, weaponThreeY, "RAM")
    canvas.drawCentredString(accX, weaponThreeY, "+0")
    canvas.drawString(damX, weaponThreeY, "X14")
    canvas.drawString(specialX, weaponThreeY, "AC")
    #canvas.drawRightString(ammoX, weaponThreeY, "O"*6)
    
def drawSkills(canvas, origin):
    
    boxWidth = 32
    boxHeight = 32
    boxLeftX = (origin[0]) + padding
    boxTopY = (origin[1]) + boxHeight + padding + 106
    
    canvas.setFillColor(white)
    canvas.roundRect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, radius=1, fill=1)
    canvas.line(boxLeftX + (boxWidth/2),boxTopY, boxLeftX + (boxWidth/2), boxTopY - boxHeight)
        
    canvas.setStrokeColor(black)
    canvas.setFillColor(black)
       
    canvas.setFont("LS", 5)
    skillY = boxTopY - 4 - padding
    canvas.drawString(boxLeftX + padding, skillY, "ATK")
    canvas.line(boxLeftX, skillY - 2, boxLeftX + boxWidth, skillY - 2)
    skillY = skillY - 6 - padding
    canvas.drawString(boxLeftX + padding, skillY, "DEF")
    canvas.line(boxLeftX, skillY - 2, boxLeftX + boxWidth, skillY - 2)
    skillY = skillY - 6 - padding
    canvas.drawString(boxLeftX + padding, skillY, "EW")
    canvas.line(boxLeftX, skillY - 2, boxLeftX + boxWidth, skillY - 2)
    skillY = skillY - 6 - padding
    canvas.drawString(boxLeftX + padding, skillY, "LD")
    
def drawActions(canvas, origin):
    boxWidth = 32
    boxHeight = 14
    boxLeftX = (origin[0]) + padding
    boxTopY = (origin[1]) + boxHeight + padding + 86
    
    canvas.setLineCap(LINECAPS['round'])
    canvas.setFillColor(white)
    canvas.rect(boxLeftX, boxTopY - boxHeight, boxWidth, boxHeight, fill=1)
    
    canvas.setFillColor(black)
    canvas.roundRect(boxLeftX, boxTopY-6, boxWidth, 6, radius=1, stroke=0, fill=1)
    canvas.line(boxLeftX, boxTopY-6, boxLeftX+boxWidth, boxTopY-6)
    canvas.setFillColor(white)
    
    canvas.setFont("LS", 4)
    canvas.drawString(boxLeftX + padding, boxTopY-5, "Actions")
    
    canvas.setStrokeColor(black)
    actionY = boxTopY - 12
    boxY = boxLeftX + padding
    canvas.rect(boxY, actionY, 4, 4)

    boxY = boxY + 8 
    canvas.rect(boxY, actionY, 4, 4)

    canvas.setStrokeColor(lightgrey)
    boxY = boxY + 8
    canvas.rect(boxY, actionY, 4, 4)

    boxY = boxY + 8
    canvas.rect(boxY, actionY, 4, 4)

def drawPerksBox(canvas, perkList, origin, boxWidth):
    print "Drawing perks box of %i width at position: (%i,%i)" % (boxWidth, origin[0], origin[1])
    
    lineHeight = 3
    origin_x = origin[0]
    origin_y = origin[1] - (lineHeight * 2)

    boxHeight = (2 * lineHeight) + 1
    secY = origin_y + 1
    priY = secY + lineHeight

    # Draw the outline
    canvas.setFillColor(white)
    canvas.setStrokeColor(black)
    canvas.rect(origin_x, origin_y, boxWidth, boxHeight, stroke=1, fill=1)

    canvas.setFillColor(black)
    canvas.setFont("LS", 3)
    perkString = ", ".join(perkList)

    # Determine if we need to wrap the text
    wrapWidth = boxWidth - (2 * padding)
    if len(perkString) * 2 > wrapWidth: # characters tend to take 2 pixels
        print "Line it too long, trying to truncate"
        index = perkString.rfind(",", 0, wrapWidth/2)
        lineOne = perkString[0:index].strip()
        print "LineOne is: ", lineOne
        canvas.drawString(origin[0] + padding, priY, lineOne)

        lineTwo = perkString[index+1:].strip()
        print "LineTwo is: ", lineTwo
        canvas.drawString(origin[0] + padding, secY, lineTwo)
    else:
        canvas.drawString(origin[0] + padding, priY, perkString)

if __name__ == '__main__':
    # Create the canvas we use as the 'file'
    c = canvas.Canvas("test.pdf", pagesize=(cWidth, cHeight))
    #c = canvas.Canvas("test.pdf", pagesize=(2*cWidth, cHeight))
    
    # Start mocking up origin to rewrite sizing based upon adjustable cards
    #   This will let us do 8-up pages, for instance.
    origin = (0,0)
    drawBorder(c, origin)
    drawUnitPicture(c, origin)
    drawBackground(c, origin)
    drawTitle(c, origin)
    drawArmor(c, origin)
    drawSkills(c, origin)
    drawActions(c, origin)
    drawMovement(c, origin)
    drawDefense(c, origin)
    drawEW(c, origin)
    drawRangedWeapons(c, origin)
    drawThrownWeapons(c, origin)
    drawMeleeWeapons(c, origin)
    
    #origin = (cWidth,0)
    #drawBorder(c)
    #drawUnitPicture(c)
    #drawBackground(c)
    #drawTitle(c)
    #drawArmor(c)
    #drawSkills(c)
    #drawActions(c)
    #drawMovement(c, origin)
    #drawDefense(c, origin)
    #drawEW(c)
    #drawRangedWeapons(c)
    #drawThrownWeapons(c)
    #drawMeleeWeapons(c)
    
    c.showPage()

    # Save the output and exit
    c.save()
    print "Done!"
